package com.housediyadmin.view.components
{
	import com.adobe.cairngorm.popup.PopUpEvent;
	import com.adobe.cairngorm.popup.PopUpWrapper;
	import com.housediyadmin.events.roomEvents.AddRoomEvent;
	import com.housediyadmin.events.roomEvents.EditRoomEvent;
	import com.housediyadmin.model.RoomModel;
	import com.housediyadmin.vo.Room;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	import mx.core.IFlexDisplayObject;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2010/12/23
	 */	
	public class RoomPopUpMediator extends Mediator
	{
		[Inject]
		public var roomModel:RoomModel;
		
		[Inject]
		public var view:RoomPopUp;
		
		[Bindable]
		public var fileName:String;
		
		public var file:FileReference;		
		public var request:URLRequest;	

		override public function onRegister():void
		{			
			request = new URLRequest("http://localhost/adminhousediy/RoomUploadService.aspx");
			//request = new URLRequest("http://10.60.1.120/HouseDiyAdmin/RoomUploadService.aspx");
			file = new FileReference();
			file.addEventListener(Event.SELECT, selectFileHandler);
			file.addEventListener(ProgressEvent.PROGRESS, uploadProgressHandler);
			view.browseBtn.addEventListener(MouseEvent.CLICK, browseFileHandler);
			
			BindingUtils.bindProperty(view.roomCreatorTI, "text", roomModel.currentRoom, "roomCreator");	
			BindingUtils.bindProperty(view.roomDescTI, "text", roomModel.currentRoom, "roomDesc");		
			BindingUtils.bindProperty(view.roomNameTI, "text", roomModel.currentRoom, "roomName");		
			BindingUtils.bindProperty(view.roomEditorTI, "text", roomModel.currentRoom, "roomEditor");
			BindingUtils.bindProperty(view.roomPicSrc, "text", roomModel.currentRoom, "roomPicSrc");
			if(view.currentState == "add")
			{
				view.addBtn.addEventListener(MouseEvent.CLICK, addRoomHandler);
			}
			if(view.currentState == "edit")
			{
				view.addBtn.addEventListener(MouseEvent.CLICK, editRoomHandler);
			}
			view.cancleBtn.addEventListener(MouseEvent.CLICK, cancleHandler);
		}
		
		private function editRoomHandler(event:MouseEvent):void
		{
			//view.roomDescTI.text = roomModel.currentRoom.roomDesc;
			roomModel.currentRoom.roomDesc = view.roomDescTI.text;
			roomModel.currentRoom.roomPicSrc = view.roomPicSrc.text;
			roomModel.currentRoom.roomEditor = view.roomEditorTI.text;
			roomModel.currentRoom.roomName = view.roomNameTI.text;
			roomModel.currentRoom.roomEditDate = new Date();
			dispatch(new EditRoomEvent(EditRoomEvent.EDIT_ROOM, roomModel.currentRoom));
			roomModel.currentRoom = new Room();
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);
			view.currentState = "edit";
		}
		
		public function browseFileHandler(event:MouseEvent):void
		{
			file.browse(getTypes());
		}
		
		private function getTypes():Array
		{
			var allTypes:Array = new Array(getImageTypeFilter());
			return allTypes;
		}
		
		private function getImageTypeFilter():FileFilter
		{
			return new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
		}
		
		public function selectFileHandler(event:Event):void
		{
			view.uploadBar.visible = true;
			file = FileReference(event.target);
			var variables:URLVariables = new URLVariables();
			variables.fileName  = "caddy" + "_" + new Date().time + file.type;
			fileName = "caddy" + "_" + new Date().time + file.type;	
			request.data = variables;
			file.upload(request);
			view.roomPicSrc.text = fileName;
			
		}			
		
		public function uploadProgressHandler(event:ProgressEvent):void
		{
			view.uploadBar.setProgress(event.bytesLoaded/event.bytesTotal*100, 100) + "%";
			view.uploadBar.label = "当前进度" + int(event.bytesLoaded/event.bytesTotal*100) + "%";
		}
		
		public function addRoomHandler(event:MouseEvent):void
		{
			var room:Room = new Room();
			room.roomCreateDate = new Date();
			room.roomEditDate = new Date();
			room.roomCreator = view.roomCreatorTI.text;
			room.roomDesc = view.roomDescTI.text;
			room.roomName = view.roomNameTI.text;
			room.roomPicSrc = view.roomPicSrc.text;
			
			dispatch(new AddRoomEvent(AddRoomEvent.ADD_ROOM, room));
			roomModel.currentRoom = new Room();
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);
			view.currentState = "add";
		}
		
		public function cancleHandler(event:MouseEvent):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);
			roomModel.currentRoom = new Room();
		}
	}
}